/*
* $Id: ChangesStore.java 234 2010-12-29 13:37:20Z volokaleks@yandex.ru $
*
* Copyright 2008-2010 by Volokitin Alexey
* All rights reserved
*
*/
package apiary.db;

import java.io.*;
import java.util.Vector;
import javax.microedition.rms.*;

/**
 * Database of changes
 * @author lexus
 */
public class ChangesStore implements RecordComparator
{
    /** Storage */
    private RecordStore store;
    /** List of records */
    private Vector records;
    /** Maximal identificator */
    private int maxId;

    /**
     * Container of record
     * @author lexus
     */
    public class ChangesRecord
    {
        public static final int TYPE_ADD_POINT = 1;
        public static final int TYPE_ADD_FAMILY = 2;
        public static final int TYPE_ADD_INSPECT = 3;
        public static final int TYPE_ADD_QUEEN_BEE = 4;
        public static final int TYPE_ADD_HOUSING = 5;
        public static final int TYPE_CHANGE_POINT = 6;
        public static final int TYPE_CHANGE_FAMILY = 7;
        public static final int TYPE_CHANGE_INSPECT = 8;
        public static final int TYPE_CHANGE_QUEEN_BEE = 9;
        public static final int TYPE_CHANGE_HOUSING = 10;
        public static final int TYPE_DELETE_POINT = 11;
        public static final int TYPE_DELETE_FAMILY = 12;
        public static final int TYPE_DELETE_INSPECT = 13;
        public static final int TYPE_DELETE_QUEEN_BEE = 14;
        public static final int TYPE_DELETE_HOUSING = 15;

        /** identificator of record */
        public int id;
        /** type of record */
        public int type;
        /** value of record */
        public byte[] value;
    }

    /**
     * Constructor
     */
    public ChangesStore() {
        try {
            maxId = 0;
            records = new Vector();
            store = RecordStore.openRecordStore("changes_store", true);
            readStore();
        } catch (RecordStoreException ex) {
        } catch (IOException ex) {}
    }

    /**
     * Gets list of records
     * @return list of records
     */
    public Vector getRecords() {
        return records;
    }

    /**
     * Adds change in database
     * @param type type of change
     * @param value value of change
     */
    public void addChange(int type, byte[] value) {
        try {
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            DataOutputStream dos = new DataOutputStream(baos);
            dos.writeInt(maxId + 1);
            dos.writeInt(type);
            dos.write(value, 0, value.length);
            byte[] array = baos.toByteArray();
            store.addRecord(array, 0, array.length);
            maxId++;
        } catch (RecordStoreException ex) {
        } catch (IOException ex) {}
    }

    /**
     * Reads database store
     */
    private void readStore() throws RecordStoreException,
                                    IOException {
        RecordEnumeration enumerator = store.enumerateRecords (null, this, false);
        while(enumerator.hasNextElement()) {
            int id = enumerator.nextRecordId();
            byte[] data = store.getRecord(id);
            try {
                ChangesRecord record = new ChangesRecord();
                ByteArrayInputStream bais = new ByteArrayInputStream(data);
                DataInputStream dis = new DataInputStream(bais);
                record.id = dis.readInt();
                record.type= dis.readInt();
                record.value = new byte[dis.available()];
                dis.read(record.value);
                records.addElement(record);
                if(maxId < record.id)
                    maxId = record.id;
            } catch (IOException ex) {}
        }
    }

    /**
     * Compares two records with each other
     * @param rec1 first record
     * @param rec2 second record
     * @return if first more than second return 1
     * if first less than second return -1
     * if first is equal second return 0
     */
    public int compare(byte[] rec1, byte[] rec2) {
        int id1 = 0, id2 = 0;
        try {
            ByteArrayInputStream bais = new ByteArrayInputStream(rec1);
            DataInputStream dis = new DataInputStream(bais);
            id1 = dis.readInt();
            bais = new ByteArrayInputStream(rec2);
            dis = new DataInputStream(bais);
            id2 = dis.readInt();
        } catch (IOException ex) {}
        if (id1 < id2)
            return -1;
        else if (id1 > id2)
            return 1;
        return 0;
    }
}
